/**
 * 
 */
package com.template;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.template.utils.TStringUtils;
import com.template.utils.Utils;

/**   
*    
* 项目名称：admin   
* 类名称：THtml   
* 类描述：自动生成基于Thymeleaf的列表页面   
* 创建人：henry   
* 创建时间：Jul 5, 2019 4:13:52 PM   
* @version        
*/
public class THtml {
	
	
	public static void createXMLFile(String tableName, String packageName,List<TableBean>columnList,List<TableBean>priList, List<TableBean>noPriList, String folderName, String path) {
		String className = TStringUtils.tableName2ClassName(tableName);
		String attrName = TStringUtils.columnName2Attr(tableName);
		String str = "";
		
		String hintstr = "";
		String contentstr = "";
		String editstr = ""; 
		String addstr ="";
		for(int i=0;i<noPriList.size();i++) {
			hintstr += "<th>"+noPriList.get(i).getColumnComment()+"</th>\r\n";
		}
		
		for(int i=0;i<noPriList.size();i++) {
            contentstr += "<td th:text=\"${"+attrName+"."+noPriList.get(i).getColumnName()+"}\">"+noPriList.get(i).getColumnComment()+"</td>\r\n";
		}
		
		for(int i=0;i<columnList.size();i++) {
			editstr +="\\''+${"+attrName+"."+columnList.get(i).getColumnName()+"}+'\\'"+(i==columnList.size()-1?"":",");
		}
		for(int i=0;i<noPriList.size();i++) {
			addstr += "<div class=\"form-group\">\r\n";
			addstr += "   <label for=\""+noPriList.get(i).getColumnName()+"\" class=\"col-sm-2 control-label\">"+noPriList.get(i).getColumnComment()+"</label>\r\n";
			addstr += "   <div class=\"col-sm-10\">\r\n";
			addstr += "   <input type=\"text\" class=\"form-control\" name=\""+noPriList.get(i).getColumnName()+"\" value=\"\" id=\""+noPriList.get(i).getColumnName()+"\" placeholder=\""+noPriList.get(i).getColumnComment()+"\">\r\n";
			addstr += " </div>\r\n";
			addstr += "</div>\r\n";
		}
		
		
		str +="<!DOCTYPE html>\r\n";
		str +="   <html>\r\n";
		str +="   <head th:include=\"include/css-head :: css-head\">\r\n";
		str +="   </head>\r\n";		
		str +="   <style>\r\n";	
		str +="        textarea{\r\n";	
		str +="          width:100%;\r\n";
		str +="          height:100px;\r\n";
		str +="         }\r\n";
		str +="   </style>\r\n";
		str +="<body>\r\n";
		str +="<!-- Content Header (Page header) -->\r\n";
		str +="<section class=\"content-header\" style=\"height: 45px\">\r\n";
		str +=" <ol class=\"breadcrumb\">\r\n";
		str +="     <li><a href=\"#\"><i class=\"fa fa-dashboard\"></i> 系统管理</a></li>\r\n";
		str +="     <li class=\"active\">产品管理</li>\r\n";
		str +=" </ol>\r\n";
		str +="</section>\r\n\r\n";
		str +="<!-- Main content -->\r\n";
		str +="<section class=\"content\">\r\n";
		str +="     <div class=\"box\">\r\n";
		str +="        <div class=\"box-header\">\r\n";
		str +="           <h3 class=\"box-title\">"+Utils.getTableComments(tableName)+"</h3>\r\n";
		str +="           <br><br>\r\n";
		str +="           <button th:if=\"${QX.add == '1' && QX.query == '1'}\" class=\"btn btn-success btn-sm\" id=\"add"+className+"\"><i class=\"fa fa-plus\"></i> &nbsp;&nbsp;添加</button>\r\n";
		str +="      </div>\r\n";
		str +="      <!-- /.box-header -->\r\n";
		str +="      <div class=\"box-body\">\r\n";
		str +="        <table id=\""+attrName+"List\" class=\"table table-bordered table-striped\">\r\n";
		str +="           <thead>\r\n";
		str +="             <tr>\r\n";
		str +="               <th>序号</th>\r\n";
        str +=hintstr;
        str +="               <th>操作</th>\r\n";
        str +="             </tr>\r\n";
        str +="          </thead>\r\n";		
        str +="    <tbody>\r\n";
        str +="    <tr th:if=\"${QX.query == '1'}\" th:each=\""+attrName+",stat :${"+attrName+"s}\">\r\n";
        str +="        <td th:text=\"${stat.count}\">序号</td>\r\n";
        str +=contentstr;
        str +="    <td>\r\n";
        str +="      <span class=\"btn btn-xs btn-danger\" th:if=\"${QX.del == '1'}\"  th:onclick=\"'del"+className+"(\\''+${"+attrName+"."+columnList.get(0).getColumnName()+"}+'\\');'\"><i class=\"fa fa-trash-o\"></i> 删除</span>\r\n";
        str +="      <span class=\"btn btn-xs btn-info\" th:if=\"${QX.edit == '1'}\" th:onclick=\"'edit"+className+"(";
        str +=editstr;
        str +=");'\"><i class=\"fa fa-edit\"></i> 编辑</span>\r\n";
        str +="      <span data-toggle=\"tooltip\" title=\"权限不够\" th:unless=\"${QX.del == '1' && QX.edit == '1'}\" ><i class=\"fa fa-lock\"></i> 无权限</span>\r\n";
        str +="    </td>\r\n";
        str +="  </tr>\r\n";
        str +="<tr th:unless=\"${QX.query == '1'}\" >\r\n";
        str +="   <td colspan=\"7\" align=\"center\">\r\n";
        str +="     <h2>此用户无权限查看该页面</h2>\r\n";
        str +="   </td>\r\n";
        str +="</tr>\r\n";
        str +=" </tbody>\r\n";
        str +="</table>\r\n\r\n";
        str +="</div>\r\n";
        str +="</section>\r\n\r\n\r\n";
        str +="<!-- 新增或修改的模态框 -->\r\n";
        str +="<div class=\"modal fade\" id=\""+attrName+"Modal\" tabindex=\"-1\" role=\"dialog\"\r\n";
        str +="   aria-labelledby=\"myModalLabel\" aria-hidden=\"true\">\r\n";
        str +="   <div class=\"modal-dialog\">\r\n";
        str +="      <div class=\"modal-content\">\r\n";
        str +="          <div class=\"modal-header\">\r\n";
        str +="              <button type=\"button\" class=\"close\" data-dismiss=\"modal\"\r\n";
        str +="                  aria-hidden=\"true\">&times;</button>\r\n";
        str +="              <h4 class=\"modal-title text-center\" id=\""+attrName+"modelHead\">添加</h4>\r\n";
        str +="           </div>\r\n";
        str +="         <div class=\"modal-body\">\r\n";
        str +="             <form class=\"form-horizontal\" role=\"form\">\r\n";
        str +="                 <input type=\"hidden\" name=\"url\" value=\"/\"/>\r\n";
        for(int i=0;i<priList.size();i++){
        str +="                 <input type=\"hidden\" name=\""+priList.get(i).getColumnName()+"\" value=\"/\"/>\r\n";	
        }
        str += addstr;
        str +="               </form>\r\n";
        str +="            </div>\r\n";
        str +="            <div class=\"modal-footer\">\r\n";
        str +="               <button type=\"button\" class=\"btn btn-primary\" data-dismiss=\"modal\">关闭</button>\r\n";
		str +="               <button type=\"button\" class=\"btn btn-success\" id=\"submit-"+attrName+"Btn\" >添加</button>\r\n";
        str +="            </div>\r\n";
        str +="        </div>\r\n";
        str +="        </div>\r\n";
        str +="        </div>\r\n\r\n";
        str +="<script th:src=\"@{/bower_components/jquery/dist/jquery.min.js}\"></script>\r\n";
        str +="<script th:src=\"@{/bower_components/bootstrap/dist/js/bootstrap.min.js}\"></script>\r\n";
        str +="<script th:src=\"@{/bower_components/datatables.net/js/jquery.dataTables.min.js}\"></script>\r\n";
        str +="<script th:src=\"@{/ztree/js/jquery.ztree.core.js}\"></script>\r\n";
        str +="<script th:src=\"@{/ztree/js/jquery.ztree.excheck.min.js}\"></script>\r\n";
        str +="<!-- AdminLTE App -->\r\n";
        str +="<script th:src=\"@{/dist/js/adminlte.min.js}\"></script>\r\n";
        str +="<script th:src=\"@{/js/path.js}\"></script>\r\n";
        str +="<script th:src=\"@{/js/"+attrName+".js}\"></script>";
        str +="</body>\r\n";
        str +="</html>\r\n";

        		
		try {
			String allPath = path+"\\"+attrName+"\\list.html";
			OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(allPath),"UTF-8");
			output.write(str);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
